package org.wkg;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.wkg.utils.DBUtil;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Demo07_ALL {
    //DriverManager
    public static void main1(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=" +
                    "utf8&allowPublicKeyRetrieval=true&useSSL=false ", "root", "123456");
            statement = connection.createStatement();
            System.out.print("请输入要查找的人：");
            Scanner scanner = new Scanner(System.in);
            String Name = scanner.next();
            String sql = "select id, name, sno, age, gender, enroll_date, class_id from student where name = '" + Name + "'";
            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                // 获取ID列的值
                long id = resultSet.getLong(1);
                // 获取name列的值
                String name = resultSet.getString(2);
                String sno = resultSet.getString(3);
                int age = resultSet.getInt(4);
                byte gender = resultSet.getByte(5);
                Date enrollDate = resultSet.getDate(6);
                long classId = resultSet.getLong(7);
                System.out.println(MessageFormat.format("学生编号={0}, 姓名={1}, 学号={2}, 年龄={3}, 性别={4}, 入学时间={5}, " +
                        "班级编号={6}", id, name, sno, age, gender, enrollDate, classId));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
// 释放结果集对象
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            // 释放Statement
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            // 关闭数据库连接
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //DateSource
    public static void main(String[] args) {
        MysqlDataSource mysqlDataSource = new MysqlDataSource();
        mysqlDataSource.setURL("jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false ");
        mysqlDataSource.setUser("root");
        mysqlDataSource.setPassword("123456");
        DataSource dataSource = mysqlDataSource;

        Connection connection = null;
        PreparedStatement statement = null;
        Scanner scanner = new Scanner(System.in);
        try {
            connection = dataSource.getConnection();
            String sql = "insert into bank_account (name, balance) values (?,?)";
            statement = connection.prepareStatement(sql);
            System.out.println("请输入要写入的姓名：");
            String name = scanner.next();
            System.out.println("请输入写入工资：");
            BigDecimal banlance = scanner.nextBigDecimal();
            statement.setString(1,name);
            statement.setBigDecimal(2,banlance);
            int row=statement.executeUpdate();
            if (row==1){
                System.out.println("插入成功");
            }else {
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(null,statement,connection);
        }

    }


}